import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class IOSAlertDialogController extends GetxController {
  // 显示弹窗
  void showIOSDialog({
    required String title,
    required String content,
    required VoidCallback onConfirm,
    VoidCallback? onCancel,
  }) {
    Get.dialog(
      CupertinoAlertDialog(
        title: Text(
          title,
          style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
        content: Padding(
          padding: const EdgeInsets.only(top: 8.0),
          child: Text(
            content,
            style: const TextStyle(fontSize: 16),
          ),
        ),
        actions: [
          // 取消按钮
          CupertinoDialogAction(
            isDefaultAction: true,
            onPressed: () {
              Get.back(); // 关闭弹窗
              onCancel?.call();
            },
            child: const Text(
              "取消",
              style: TextStyle(color: CupertinoColors.systemGrey),
            ),
          ),
          // 确认按钮
          CupertinoDialogAction(
            isDestructiveAction: true,
            onPressed: () {
              Get.back(); // 关闭弹窗
              onConfirm();
            },
            child: const Text(
              "确认",
              style: TextStyle(color: CupertinoColors.systemRed),
            ),
          ),
        ],
      ),
      barrierDismissible: false, // 禁止点击空白关闭
    );
  }
}
class HomePage extends StatelessWidget {
  HomePage({Key? key}) : super(key: key);

  final IOSAlertDialogController controller = Get.put(IOSAlertDialogController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("GetX iOS 弹窗示例")),
      body: Center(
        child: CupertinoButton.filled(
          onPressed: () {
            controller.showIOSDialog(
              title: "提示",
              content: "确定要删除这条记录吗？",
              onConfirm: () {
                Get.snackbar("结果", "已确认删除");
              },
              onCancel: () {
                Get.snackbar("结果", "取消操作");
              },
            );

          },
          child: const Text("显示弹窗"),
        ),
      ),
    );
  }
}
